package repository

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/FileName"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Slice"
	"os"
)

type repoLocalPathNameService struct {
}

func (e *repoLocalPathNameService) findRepoLocalModelType(modelType string) []map[string]interface{} {
	resultList := make([]map[string]interface{}, 0)

	// 仓库目录
	repoDirName := FileName.GetOsFilePath(commEnv.Service.WorkDir + "/repository")
	typeDirName := FileName.GetOsFilePath(repoDirName + "/" + modelType)

	// 类型级别的目录：\opt\fox-edge\repository\decoder
	inf, err := os.Stat(typeDirName)
	if err != nil {
		return resultList
	}
	if !inf.IsDir() {
		return resultList
	}

	modelFiles, err := FileName.List(typeDirName)
	if err != nil {
		return resultList
	}

	for _, modelFile := range modelFiles {
		inf, err = os.Stat(FileName.GetOsFilePath(typeDirName + "/" + modelFile))
		if err != nil {
			continue
		}
		if !inf.IsDir() {
			continue
		}

		versions := e.findRepoLocalModelName(modelType, modelFile)

		resultList = Slice.Append(resultList, versions)
	}

	return resultList
}

func (e *repoLocalPathNameService) getFileName4LocalRepoTarFile(modelName, version, ext string) string {
	return modelName + "-" + version + ext
}

func (e *repoLocalPathNameService) getPathName4LocalRepo2component(modelType, modelName, version, stage, component string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/repository/" + modelType + "/" + modelName + "/" + version + "/" + stage + "/" + component)
}
func (e *repoLocalPathNameService) getPathName4LocalRepo2tarFile(modelType, modelName, version, stage, component, ext string) string {
	return FileName.GetOsFilePath(e.getPathName4LocalRepo2component(modelType, modelName, version, stage, component) + "/" + e.getFileName4LocalRepoTarFile(modelName, version, ext))
}

func (e *repoLocalPathNameService) getPathName4LocalRepo2tar(modelType, modelName, version, stage, component string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/repository/" + modelType + "/" + modelName + "/" + version + "/" + stage + "/" + component + "/" + "tar")
}

func (e *repoLocalPathNameService) getPathName4LocalJarDecoder2file(modelName string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/jar/decoder/" + modelName + ".jar")
}

func (e *repoLocalPathNameService) getPathName4LocalRepo2modelName(modelType, modelName string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/repository/" + modelType + "/" + modelName)
}

func (e *repoLocalPathNameService) getPathName4LocalBin2MainFile(appType, appName, mainFileName string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/bin/" + appType + "/" + appName + "/" + mainFileName)
}

func (e *repoLocalPathNameService) GetPathName4LocalShell2confFile(appType, appName string) string {
	return FileName.GetOsFilePath(commEnv.Service.WorkDir + "/shell/" + appType + "/" + appName + "/" + "service.conf")
}

func (e *repoLocalPathNameService) findRepoLocalRepoTarFile(modelType, modelName, version, stage, component string) string {
	packFile := ""
	tarFile := LocalPathName.getPathName4LocalRepo2tarFile(modelType, modelName, version, stage, component, ".tar")
	tarGzFile := LocalPathName.getPathName4LocalRepo2tarFile(modelType, modelName, version, stage, component, ".tar.gz")
	if FileName.IsFile(tarGzFile) {
		packFile = tarGzFile
	}
	if FileName.IsFile(tarFile) {
		packFile = tarFile
	}

	return packFile
}

func (e *repoLocalPathNameService) findRepoLocalModelName(modelType string, modelName string) []map[string]interface{} {
	resultList := make([]map[string]interface{}, 0)

	// 仓库目录
	modelVersionDir := FileName.GetOsFilePath(commEnv.Service.WorkDir + "/repository/" + modelType + "/" + modelName)

	// 类型级别的目录：\opt\fox-edge\repository\decoder
	inf, err := os.Stat(modelVersionDir)
	if err != nil {
		return resultList
	}
	if !inf.IsDir() {
		return resultList
	}

	versionFiles, err := FileName.List(modelVersionDir)
	if err != nil {
		return resultList
	}

	for _, versionFile := range versionFiles {
		versionPath := FileName.GetOsFilePath(modelVersionDir + "/" + versionFile)
		inf, err = os.Stat(versionPath)
		if err != nil {
			continue
		}
		if !inf.IsDir() {
			continue
		}

		stageFiles, err := FileName.List(versionPath)
		if err != nil {
			continue
		}

		for _, stageFile := range stageFiles {
			stagePath := FileName.GetOsFilePath(versionPath + "/" + stageFile)
			inf, err = os.Stat(stagePath)
			if err != nil {
				continue
			}
			if !inf.IsDir() {
				continue
			}

			componentFiles, err := FileName.List(stagePath)
			if err != nil {
				continue
			}

			for _, componentFile := range componentFiles {
				componentPath := FileName.GetOsFilePath(stagePath + "/" + componentFile)
				inf, err = os.Stat(componentPath)
				if err != nil {
					continue
				}
				if !inf.IsDir() {
					continue
				}

				// 提取业务参数
				version := versionFile
				stage := stageFile
				component := componentFile

				data := make(map[string]interface{})
				data["modelType"] = modelType
				data["modelName"] = modelName
				data["version"] = version
				data["stage"] = stage
				data["component"] = component

				resultList = append(resultList, data)

			}

		}

	}

	return resultList
}
